﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.mojoPortalExtend.Urls;
using blackhouse.NemoBlog.Business;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.SearchIndex;
using mojoPortal.Web.Framework;

namespace blackhouse.NemoBlog.Web {
    public class PostExtension : RecordExtension, IIndexableContent {

        #region Fields

        private SiteSettings currentSite;
        private NemoBlogSettings settings;

        #endregion

        public PostExtension(GroupRecord record)
            : base(record) {
            IndexBuilderProvider indexBuilder = IndexBuilderManager.Providers["PostIndexBuilderProvider"];
            if (ConfigHelper.GetBoolProperty("DisableSearchIndex", false) && indexBuilder != null)
                this.ContentChanged += new ContentChangedEventHandler(indexBuilder.ContentChangedHandler);
            this.currentSite = CacheHelper.GetCurrentSiteSettings();
        }

        protected override string GetIdentyficator(GroupRecord record) {
            return record.GetString("title");
        }

        public override bool ShowOnRecordsList(GroupRecord record) {
            if (record.IsNull("site"))
                return false;
            else
                return record.GetInt32("site") == this.currentSite.SiteId;
        }

        protected override void RecordChanged(GroupRecord record, RecordChangeType type) {
            NemoBlogPost post = new NemoBlogPost(record.RecordId);
            this.settings = new NemoBlogSettings(post.ModuleId);
            if (type == RecordChangeType.Added) {
                post.PostGuid = Guid.NewGuid();
                int mid = 0;
                if (HttpContext.Current != null && Int32.TryParse(HttpContext.Current.Request["mid"], out mid)) {
                    post.ModuleId = mid;
                }
                post.SetValue("site", this.currentSite.SiteId);
                post.Save(false);
            }

            if (type == RecordChangeType.Added || type == RecordChangeType.Updated) {
                PostUrlSupport postUrl = new PostUrlSupport(post);
                if (this.settings.Exists && this.settings.AllowUserChangeUrl && !String.IsNullOrEmpty(post.PostUrl))
                    postUrl.SetUserNewUrl(PostUrlSupport.FormatUrl(post.PostUrl));
                UrlSupportProvider urlProvider = new UrlSupportProvider(postUrl);
                urlProvider.Save();
            }

            if (type == RecordChangeType.Deleted) {
                FriendlyUrl urlManage = new FriendlyUrl(this.currentSite.SiteId, record.GetString("url").ReplaceEx("~/", ""));
                if (urlManage.FoundFriendlyUrl)
                    FriendlyUrl.DeleteUrl(urlManage.UrlId);
            }

            ContentChangedEventArgs e = new ContentChangedEventArgs();
            if (type == RecordChangeType.Deleted)
                e.IsDeleted = true;
            OnContentChanged(record, e);

        }

        #region IIndexableContent

        public event ContentChangedEventHandler ContentChanged;

        protected void OnContentChanged(GroupRecord record, ContentChangedEventArgs e) {
            if (ContentChanged != null) {
                ContentChanged(record, e);
            }
        }

        #endregion

    }
}